package com.frostmourne.mxsguimaker.objects;

import com.frostmourne.mxsguimaker.MXSGuiMaker;
import com.gitee.mxstarsex.objects.builders.bukkit.item.ItemBuilder;
import org.apache.commons.lang3.RandomUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PlayerData {

    public final static Map<String,PlayerData> DATA_MAP = new HashMap<>();
    private final Map<String,Object> foreverObjects = new HashMap<>();

    public static PlayerData getPlayerData(String name){
        return DATA_MAP.getOrDefault(name,null);
    }
    public static PlayerData getPlayerData(Player player){
        return DATA_MAP.getOrDefault(player.getName(),null);
    }


    private final Map<String,Map<Integer, ItemStack>> inventoryItems = new HashMap<>();
    private final String name;
    private final Map<String,Object> tempObjectMap = new HashMap<>();

    public PlayerData(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public Object getTempObject(String name){
        return tempObjectMap.getOrDefault(name,null);
    }
    public void putTempObject(String name,Object object){
        tempObjectMap.put(name,object);
    }

    public Object getForeverObject(String name){
        return foreverObjects.getOrDefault(name,null);
    }
    public void putForeverObject(String name,Object object){
        foreverObjects.put(name,object);
    }

    public List<ItemStack> getItems(String inventoryName){
        return new ArrayList<>(inventoryItems.getOrDefault(inventoryName,new HashMap<>()).values());
    }

    public void setItems(String inventoryName,Map<Integer,ItemStack> itemStackMap){

        inventoryItems.put(inventoryName,itemStackMap);
    }

    public ItemStack getItem(String inventoryName,int slot){
        return inventoryItems.getOrDefault(inventoryName,new HashMap<>()).getOrDefault(slot,null);
    }
    public Map<Integer, ItemStack> getItemMap(String inventoryName){
       return inventoryItems.getOrDefault(inventoryName,new HashMap<>());
    }

    public void load(){

        File dataFile = getDataFile();
        if (dataFile.exists()){
            FileConfiguration config = YamlConfiguration.loadConfiguration(dataFile);

            if (config.getKeys(false).contains("InvData")){

                System.out.println("检测到存档为 MXSAttributeInventory 存档");
                System.out.println("开始进行数据转换");
                for (String invName : config.getConfigurationSection("InvData").getKeys(false)){
                    for (String slotStr : config.getConfigurationSection("InvData." + invName).getKeys(false)){
                        int slot = Integer.parseInt(slotStr);

                        ItemStack itemStack = config.getItemStack("InvData." + invName + "." + slot);

                        Map<Integer, ItemStack> itemMap = this.inventoryItems.getOrDefault(invName, new HashMap<>());
                        itemMap.put(slot,itemStack);

                        this.inventoryItems.put(invName,itemMap);


                    }
                }
                System.out.println("数据转换完成");

            }

            if (config.getKeys(false).contains("data")){
                ConfigurationSection dataConfig = config.getConfigurationSection("data");
                if (dataConfig.getKeys(false).contains("items")){

                    for (String inventoryName : dataConfig.getConfigurationSection("items").getKeys(false)){

                        Map<Integer,ItemStack> itemStackMap = new HashMap<>();
                        for (String slot : dataConfig.getConfigurationSection("items." + inventoryName).getKeys(false)){
                            itemStackMap.put(
                                    Integer.parseInt(slot),
                                    ItemBuilder.deserializeToItemStack(dataConfig.getString("items." + inventoryName + "." + slot)))
                            ;
                        }
                        this.inventoryItems.put(inventoryName,itemStackMap);

                    }

                }


                if (dataConfig.getKeys(false).contains("forever-objects")){
                    for (String name : dataConfig.getConfigurationSection("forever-objects").getKeys(false)){
                        this.foreverObjects.put(name,dataConfig.get("forever-objects." + name));
                    }
                }
            }
        }
    }

    public void save(){

        File dataFile = getDataFile();

        FileConfiguration config = YamlConfiguration.loadConfiguration(dataFile);
        config.set("data",null);
        config.set("InvData",null);

        for (String s : inventoryItems.keySet()){
            Map<Integer, ItemStack> itemStackMap = inventoryItems.get(s);
            for (int i : itemStackMap.keySet()){
                config.set("data.items." + s + "." + i,ItemBuilder.serialize(itemStackMap.get(i)));
            }
        }
        for (String s : foreverObjects.keySet()){
            config.set("data.forever-objects." + s ,foreverObjects.get(s));
        }

        try {
            config.save(dataFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    public File getDataFile(){
        return new File(MXSGuiMaker.INSTANCE.getDataFolder() + "/data",this.name + ".yml");
    }



}
